remove(list=ls())
setwd(dirname(rstudioapi::getActiveDocumentContext()$path))

# Create Quarterly KZ index
library(tidyverse)
library(data.table)
library(here)
library(DescTools)
wins <- function(x, p = c(0.01, 0.99), na.rm = TRUE) {
  q <- quantile(x, probs = p, na.rm = na.rm)
  x[x < q[1]] <- q[1]
  x[x > q[2]] <- q[2]
  return(x)
}

# 1. read and prepare data ----------------------------------

# read monthly CRSP data
df_monthlyCRSP <- fread("../data/monthlyCRSP.csv")

df_monthlyCRSP <- df_monthlyCRSP %>% mutate(time_avail_m = ymd(time_avail_m))

df_monthlyCRSP <- df_monthlyCRSP %>% mutate(year = year(time_avail_m),
                                            month = month(time_avail_m))

# read annual compustat data
df_CompustatAnnual <- readRDS("../data/a_aCompustat.RDS")

df_CompustatAnnual <- df_CompustatAnnual %>% mutate(datadate = ymd(datadate))

# 2. Construct main sample ------------------

df_main <- df_CompustatAnnual

# link compustat with monthly CRSP
df_main <- df_main %>% mutate(year = year(datadate),
                              month = month(datadate))

df_main <- df_main %>% 
  inner_join(df_monthlyCRSP %>% select(permno, year, month, mve_c),
             by = c("permno", "year", "month"))

# some filters
df_main <- df_main %>% 
  filter(!is.na(at)) %>% 
  filter(at>0) %>% 
  filter(ppent>0) %>% 
  filter(sale>0)

# 3. Compute KZ Index ---------------------------

df_main <- df_main %>% 
  mutate(TobinQ = (at + prcc_f*csho - ceq - txdb)/at)

## 3,1 Original KZ index ------------------------------------
df_main <- df_main %>% 
  mutate(KZ =
           -1.002* (ib + dp)/ppent + # ratio of cash flow to total assets
           .283*TobinQ + # Tobin's Q
           3.139*(dlc + dltt)/(dlc + dltt + seq) + # ratio of long-term debt to total assets
           -39.368*((dvc+dvp)/ppent) + # ratio of total dividends to assets
           -1.315*(che/ppent) # ratio of liquid assets to total assets
  )

## 3.2 KZ index without the Q component --------------------
df_main <- df_main %>%
  mutate(KZ_withoutQ =
           -1.002* (ib + dp)/ppent + # ratio of cash flow to total assets
           3.139*(dlc + dltt)/(dlc + dltt + seq) + # ratio of long-term debt to total assets
           -39.368*((dvc+dvp)/ppent) + # ratio of total dividends to assets
           -1.315*(che/ppent) # ratio of liquid assets to total assets
  )

# 4. Compute WW index ----------------------
# compute the 3-digit industry sales growth
df_main <- df_main %>% mutate(SIC_3d = str_sub(sic, 1, 3))

df_main <- df_main %>% 
  group_by(SIC_3d, year) %>% 
  mutate(IndSales_tot = sum(sale, na.rm = T)) %>% 
  ungroup()

df_main <- df_main %>% 
  group_by(SIC_3d) %>% 
  mutate(ISG = IndSales_tot/lag(IndSales_tot)-1) %>% 
  ungroup()

# compute firm's sales growth 
df_main <- df_main %>% 
  group_by(gvkey) %>% 
  mutate(SG = sale/lag(sale)-1) %>% 
  ungroup()

# create dividend indicator 
df_main <- df_main %>% mutate(dum_DIVPOS = ifelse(dvc+dvp>0 & !is.na(dvc) & !is.na(dvp), 1, 0))

# compute WW index
# (Divide CF and growth rates by 4 to approximate quarterly rates)
df_main <- df_main %>% 
  mutate(WW = 
           -0.091*(ib+dp)/(4*at) + # ratio of cash flow to total assets 
           -0.062*dum_DIVPOS + # Indicate if firm pays dividend
           0.021*dltt/at + # ratio of long-term debt to total assets
           -0.044*log(at) + # log of total assets 
           0.102*ISG + # industry sales growth
           -0.035*SG) # firm sales growth

df_main <- df_main %>% 
  mutate(
    TobinQ = wins(TobinQ),
    KZ = wins(KZ),
    KZ_withoutQ = wins(KZ_withoutQ),
    WW = wins(WW)
  )

df_main <- df_main %>% 
  mutate(TobinQ = round(TobinQ, 3),
         KZ = round(KZ, 3),
         KZ_withoutQ = round(KZ_withoutQ, 3),
         WW = round(WW, 3))


df_ConstraintMeasures <- df_main %>% 
  select(gvkey, permno, datadate, year, TobinQ, KZ, KZ_withoutQ, WW)

fwrite(df_ConstraintMeasures, "../output/ConstraintMeasures_A.csv")

